﻿using ConfExpert.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevTeam;
using DevExpress.XtraGrid.Views.Grid;
using ConfExpert.Datasets;
using ConfExpert.Utility;
using DevExpress.XtraEditors;

namespace ConfExpert.Forms.Depo.Kauf
{
    public partial class BMistekleri : XForm
    {
        int DagilimId = 0;

        public BMistekleri()
        {
            InitializeComponent();
        }

        private void BMistekleri_Load(object sender, EventArgs e)
        {
            this.Text = "BM İstekleri";
            gridView1.Init("GetBMistekleri");
            gridView1.AddDetailGrid(gridView2, "BMIstekId");

            //////gridView1.MakeColumnInvisible("BMIstekId");
            gridView1.MakeColumnInvisible("idx");
            gridView2.MakeColumnInvisible("BMIstekLinesId");
            gridView1.MakeColumnsReadOnly("BMIstekId", "Notlar", "Model", "Kalip", "Line", "DagilimId", "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28");
            //////gridView1.MakeColumnsReadOnly("Model", "Kalip", "Line", "DagilimId", "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28");
            gridView2.MakeColumnsReadOnly("Nr", "ProductId", "StokKodu", "GarniPos", "KumasEni", "KumasRaporu", "DesenYonuId");

            for (int i = 1; i <= 28; i++)
            {
                DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[string.Format("A{0:00}", i)];
                col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                col.DisplayFormat.FormatString = "P1";
            }

            DagilimChanged();

            MakeDagilimColumnsInvisible(gridView1);

            gridView1.AddSummary();

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (BMIstekDetayKayitlarDoluMu(gridView2))
            {
                gridView2.Update("", "BMIstekLines");

                //ExecuteNonQuery("UpdBMistekleri", gridView1.Serialize(), gridView2.Serialize());
            }
            else
            {
                XtraMessageBox.Show("Boş Alan Bıraktınız veya Geçersiz Karakter Girişi Yaptınız, Lütfen Tekrar Kontrol Ediniz!", "Uyarı");
            }
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DagilimChanged();
        }

        public void MakeDagilimColumnsInvisible(GridView gv)
        {
            bool ValueDetected = false, BreakSuccess = false;
            //string cellValue0, cellValue1, cellValue2, cellValue3, cellValue6;
            decimal cellValue;
            string ColumnHeader;
            int GridRowCount = gv.DataRowCount;
            for (int i = 28; i > 0; --i)
            {
                ColumnHeader = String.Format("A{0:00}", i);
                for (int j = 0; j < GridRowCount; ++j)
                {

                    cellValue = gv.GetRowCellValue(gv.GetRowHandle(j), ColumnHeader).ToDecimal();

                    if (cellValue > 0)
                    {
                        ValueDetected = true;

                    }

                    if (ValueDetected)
                    {
                        BreakSuccess = true;
                        break;
                    }
                }
                if (BreakSuccess)
                    break;
                else
                    gv.MakeColumnInvisible(ColumnHeader);
            }

            // reset variables
            ValueDetected = BreakSuccess = false;

            for (int i = 1; i < 29; ++i)
            {
                ColumnHeader = String.Format("A{0:00}", i);
                for (int j = 0; j < GridRowCount; ++j)
                {

                    cellValue = gv.GetRowCellValue(gv.GetRowHandle(j), ColumnHeader).ToDecimal();

                    if (cellValue > 0)
                    {
                        ValueDetected = true;

                    }

                    if (ValueDetected)
                    {
                        BreakSuccess = true;
                        break;
                    }
                }
                if (BreakSuccess)
                    break;
                else
                    gv.MakeColumnInvisible(ColumnHeader);
            }
        }

        private void DagilimChanged()
        {
            try
            {
                gridView1.CloseEditor();
                gridView1.BeginUpdate();
                if (DagilimId != gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DagilimId").ToInt(-1))
                {
                    DagilimId = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DagilimId").ToInt(-1);

                    MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(DagilimId);
                    if (r != null)
                        for (int i = 1; i < 29; i++)
                        {
                            string fieldName = String.Format("A{0:00}", i);
                            string cap = r[String.Format("A{0:00}", i)].ToString();
                            gridView1.Columns[fieldName].Caption = cap != string.Empty ? cap : " ";
                        }
                }

            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                gridView1.EndUpdate();
            }
        }

        public bool BMIstekDetayKayitlarDoluMu(GridView gv)
        {
            bool AllColumnsDolu = true;
            decimal cellValue1;
            string cellValue2;
            
                for (int i = 0; i < gv.DataRowCount; ++i)
                {
                    cellValue1 = gv.GetRowCellValue(gv.GetRowHandle(i), "BM").ToDecimal(-1);
                    cellValue2 = gv.GetRowCellValue(gv.GetRowHandle(i), "YikamaSonucu").ToStr();
                    if (cellValue1 == -1
                        || cellValue2.Equals(String.Empty))
                    {
                        AllColumnsDolu = false;
                        break;
                    }
                }
            
            return AllColumnsDolu;
        }
    }
}
